const Constants = require('../../utils/constants');
const Util = require('../../utils/util');
const Auth = require('../../utils/auth');
const Api = require('../../utils/api.js');
const Rest = require('../../utils/rest');
const util = require('../../utils/util.js');

Page({

    data: {
        setting: {},
    },
    default: {
        background: Api.JIANGQIE_BG_LOGIN,
        title: '',
        description: '',
    },
    onLoad: function (options) {
        //获取配置
        let that = this;
        Rest.get(Api.JIANGQIE_SETTING_LOGIN).then(res => {
            util.getshare(that);
            that.setData({
                setting: {
                    background: res.data.background ? res.data.background : that.default.background,
                    title: res.data.title ? res.data.title : that.default.title,
                    description: res.data.description ? res.data.description : that.default.description,
                }
            });
        })
        this.setData({
            title: getApp().appName
        });
    },
    onReachBottom: function () {
        if (!this.data.pullUpOn) {
            return;
        }

        this.loadPosts(false);
    },
    

    onShareAppMessage: function () {
        return {
            title: getApp().appName,
            path: 'pages/index/index',
        }
    },

    onShareTimeline: function () {
        return {
            title: getApp().appName,
        }
    },

    handlerCancelClick: function(e) {
        Util.navigateBack();
    },
    handlerLoginClick: function (e) {
        wx.getUserProfile({
            desc: '使用微信的头像昵称初始化用户',
            success: function (wxu) {
                Auth.getWXUser().then(res => {
                    return Rest.get(Api.JIANGQIE_USER_LOGIN, {
                        code: res.code,
                        nickName: wxu.userInfo.nickName,
                        avatarUrl: wxu.userInfo.avatarUrl,
                    })
                }).then(res => {
                    let user = res.data;
                    Auth.setUser(user);

                    Util.navigateBack();
                });
            },
            fail: function (err) {
                Util.toast('需要同意才能登录');
            }
        });
    }
})